// pages/transportstatistics/transportstatistics.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    id: null,
    list: {},
    itemList: [],
    ifEmpty: false
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  onChange: function(event) {
    this.toggle("active", event.detail.index)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.toggle("id", options.id);
    that.getDict();
  },
  getDict: function() {
    var that = this;
    var data = that.data;
    app.req({
      url: "wechat/dictionary/listBydictType",
      method: "post",
      data: {
        dictType: "ACT_TRANSPORT"
      },
      success: function(res) {
        if (200 == res.code) {
          var list = res.data;
          var dict = {};
          for (var i in list) {
            var item = list[i];
            dict[item.dictValue] = item.dictName;
          }
          that.toggle("dict", dict)
          that.getItem();
        }else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
            })
        }
      }
    });
  },
  getItem: function() {
    var that = this;
    var data = that.data;
    app.req({
      url: "wechat/activity/activityDetail",
      method: "post",
      data: {
        id: data.id
      },
      success: function(res) {
        if (200 == res.code) {
          var trans = res.data.transportation;
          var dict = data.dict;
          if (trans) {
            var arr = trans.split(",");
            var item = [];
            for (var i in arr) {
              item.push({
                id: arr[i],
                title: dict[arr[i]],
                list: []
              });
            }
          }

          that.toggle("itemList", item)
          that.getListData();
        }else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
            })
        }
      }
    });
  },
  getListData: function() {
    var that = this;
    var data = that.data;
    app.req({
      url: "wechat/act/participation/listActParticipantByActId",
      method: "post",
      data: {
        activityId: data.id,
        isCheck: 1
      },
      success: function(res) {
        if (200 == res.code) {
          var list = res.data;
          var tabs = data.itemList;
          var nowList = {};
          for (var i in list) {
            var item = list[i];
            var nowId = item.transportation * 1;
            for (var j in tabs) {
              var detail = tabs[j];
              if (detail.id * 1 == nowId) {
                detail.list.push(item);
              }
            }
          }
          for (let k = 0; k < tabs.length; k++) {
            if (tabs[k].list.length) {
              tabs[k].ifEmpty = false;
            } else {
              tabs[k].ifEmpty = true;
            }
          }
          that.toggle("itemList", tabs);
        }else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
            })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  onPullDownRefresh: function() {
    this.getDict();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
})